package com.atlin.project.wxmp.handler;

import com.atlin.project.service.TaskListService;
import lombok.extern.java.Log;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 消息处理器
 **/
@Component
@Log
public class MessageHandler implements WxMpMessageHandler {

    @Resource
    private TaskListService taskListService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map,
                                    WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        
        String fromUser = wxMpXmlMessage.getFromUser();
        log.info("fromUser: " + fromUser);
        String content = taskListService.getTodayTaskList(fromUser);
//        String content = "我是复读机：" + wxMpXmlMessage.getContent();
        log.info("handle: " + content);
        /**
         * oaMIp6vR_72RKROsH-sCoDbvHTFM 林
         *
         * oaMIp6k0COlhv3AnDO_y4yezptrQ 卡米
         */
        return WxMpXmlOutMessage.TEXT().content(content)
                .fromUser(wxMpXmlMessage.getToUser())
                .toUser(wxMpXmlMessage.getFromUser())
                .build();
    }
}
